﻿package com.dykj.platform.framework.common.paging;

import java.io.Serializable;

import com.dykj.platform.framework.dao.Constants;
import com.dykj.platform.framework.dao.support.Sort;

/**
 * @Title: DYKJ Platform
 * @Description: 分页查询的页面信息
 * @Copyright: Copyright (c) 2008
 * @Company: DYKJ
 * @author jit@dykj.cn
 * @version 1.0
 */
public class Page implements Serializable {

	private static final long serialVersionUID = -66553157173977857L;

	/**
	 * 页面大小
	 */
	protected int pageSize;

	/**
	 * 当前页面号码
	 */
	protected int currentPageNo;

	/**
	 * 总记录条数 int max 2147483647
	 */
	protected int totalRecordNum;

	/**
	 * 总页面数
	 */
	protected int totalPageNum;
	/**
	 * 总页面数
	 */
	protected int nextPage;
	/**
	 * 上一页
	 */
	protected int previousPage;

	/**
	 * 排序方式
	 */
	protected Sort sort;
	/**
	 * 当前用户
	 */
	protected String dataAccessAuth;
	public Page() {
		pageSize = 10;
		currentPageNo = 0;
		totalRecordNum = 0;
		totalPageNum = 0;
		sort = null;
	}

	public void init(int totalRecordNum) {
		init(Constants.DEFAULT_PAGE_SIZE, totalRecordNum);
	}

	public void init(int pageSize, int totalRecordNum) {
		init(pageSize, 1, totalRecordNum);
	}

	public void init(int pageSize, int currentPageNo, int totalRecordNum) {
		init(pageSize, currentPageNo, totalRecordNum, getTotalPageNum(
				totalRecordNum, pageSize));
	}

	public void init(int pageSize, int currentPageNo, int totalRecordNum,
			int totalPageNum) {
		if (pageSize >= 0 && currentPageNo >= 0 && totalRecordNum >= 0
				&& totalPageNum >= 0) {
			setPageSize(pageSize);
			setTotalRecordNum(totalRecordNum);
			setTotalPageNum(totalPageNum);
			setCurrentPageNo(currentPageNo);
		}
	}

	/**
	 * 第一页
	 */
	public void first() {
		if (totalPageNum > 0)
			currentPageNo = 1;
		else
			currentPageNo = 0;
	}

	/**
	 * 上一页
	 */
	public void pre() {
		if (currentPageNo > 1)
			currentPageNo--;
	}

	/**
	 * 下一页
	 */
	public void next() {
		if (currentPageNo < totalPageNum)
			currentPageNo++;
	}

	/**
	 * 最后一页
	 */
	public void last() {
		currentPageNo = totalPageNum;
	}

	/**
	 * 跳转
	 * 
	 * @param pageNum
	 *            int
	 */
	public void gotoPage(int pageNum) {
		if (pageNum > 0 && pageNum < totalPageNum)
			currentPageNo = pageNum;
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getTotalPageNum() {
		return totalPageNum;
	}

	public int getTotalRecordNum() {
		return totalRecordNum;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setTotalPageNum(int totalPageNum) {
		this.totalPageNum = totalPageNum;
	}

	public void setTotalRecordNum(int totalRecordNum) {
		this.totalRecordNum = totalRecordNum;
		if(pageSize!=0){
			totalPageNum = totalRecordNum / pageSize;
			if(totalRecordNum % pageSize != 0)
				totalPageNum++;
		}
	}

	protected int getTotalPageNum(int totalRecordNum, int pageSize) {
		int res = 0;
		if (pageSize != 0) {
			res = totalRecordNum / pageSize;
			if (totalRecordNum % pageSize != 0)
				res++;
		}
		return res;
	}

	public Sort getSort() {
		return sort;
	}

	public void setSort(Sort sort) {
		this.sort = sort;
	}

	public String getDataAccessAuth() {
		return dataAccessAuth;
	}

	public void setDataAccessAuth(String dataAccessAuth) {
		this.dataAccessAuth = dataAccessAuth;
	}

	public int getNextPage() {
		return currentPageNo+1;
	}

	public int getPreviousPage() {
		return currentPageNo-1;
	}

}
